<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bg{
            background-color: blue;
        }
        #con{
            width: 400px;
            height: 300px;
            border: 2px solid red;
            padding: 30px;;
        }
    </style>
</head>

<body>
    <div id="con" class="list"">
        <img src="animal1.png" style="width: 100%;"/>
    </div>
</body>
<script>
    var list = document.getElementsByClassName("list"); 
    list[0].onmouseover = function(){
        var classStr = this.className;
        var classArr = classStr.split(" ");
        if(classArr.indexOf("bg")>0){
            return ;
        }
        else{
            this.className = this.className + " bg";//给class赋值
        }   
    }

    list[0].onmouseout = function(){
        var classStr = this.getAttribute("class");
        var classArr = classStr.split(" ");
        var index = classArr.indexOf("bg");
        // var className1 = this.className.split(" ");
        if(index>=0){
            classArr.splice(index,1);
            var str = classArr.join(" ");
            console.log(str);
            this.setAttribute("class",str);
            // var index = className1.indexOf("bg");
            // className1 = className1.splice(index-1,1);
            // classStr1 = className1.join(" ");
            // console.log(classStr1);
        }
        else{
            return;
        } 
    }
</script>
</html>